package com.xingdata.jjxc.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class UtilTime {
	private static String mYear;
	private static String mMonth;
	private static String mDay;
	private static String mWay;

	// 1. 获取当前系统日期
	public static String getCurrentYMD() {
		Date nowTime = new Date();
		SimpleDateFormat time = new SimpleDateFormat("yyyyMMdd");
		String ymd = time.format(nowTime).toString();
		return ymd;
	}

	public static String getCurrentY() {
		Date nowTime = new Date();
		SimpleDateFormat time = new SimpleDateFormat("yyyy");
		String ymd = time.format(nowTime).toString();
		return ymd;
	}

	public static String getCurrentM() {
		Date nowTime = new Date();
		SimpleDateFormat time = new SimpleDateFormat("MM");
		String ymd = time.format(nowTime).toString();
		return ymd;
	}

	public static String getCurrentDD() {
		Date nowTime = new Date();
		SimpleDateFormat time = new SimpleDateFormat("dd");
		String ymd = time.format(nowTime).toString();
		return ymd;
	}

	// 获取系统年月
	public static String getCurrentYM() {
		Date nowTime = new Date();
		SimpleDateFormat time = new SimpleDateFormat("yyyyMM");
		String ymd = time.format(nowTime).toString();
		return ymd;
	}

	// 2. 获取当前系统时间
	public static String getCurrentHMS() {
		Date nowTime = new Date();
		SimpleDateFormat time = new SimpleDateFormat("HHmmss");
		String hms = time.format(nowTime).toString();
		return hms;
	}

	// 3. 获取当前系统日期与时间
	public static String getCurrentYMDHMS() {
		Date nowTime = new Date();
		SimpleDateFormat time = new SimpleDateFormat("yyyyMMddHHmmss");
		String ymdhms = time.format(nowTime).toString();
		return ymdhms;
	}

	// 4. 获取某天之前若干天的YMD日期
	public static String getPrevDaysYMD(String beginDate, int daysNum) {

		String str = null;
		int feb_days, month_days;

		String ys = beginDate.substring(0, 4);
		String ms = beginDate.substring(4, 6);
		String ds = beginDate.substring(6, 8);

		Integer year_i = Integer.parseInt(ys);
		Integer month_i = Integer.parseInt(ms);
		Integer date_i = Integer.parseInt(ds);

		while (daysNum > 0) {
			if (((year_i % 400) == 0)
					|| ((year_i % 4) == 0 && (year_i % 100) != 0)) {
				feb_days = 29;
			} else {
				feb_days = 28;
			}

			switch (month_i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				month_days = 31;
				break;

			case 4:
			case 6:
			case 9:
			case 11:
				month_days = 30;
				break;
			case 2:
				month_days = feb_days;
				break;
			default:
				return (null);
			}
			if (daysNum > (month_days - date_i)) {
				if (date_i == month_days) {
					daysNum = daysNum - 1;
					date_i = 1;
					month_i = month_i + 1;
				} else {
					daysNum = daysNum + date_i - month_days - 1;
					date_i = 1;
					month_i = month_i + 1;
				}

				if (month_i > 12) {
					month_i = 1;
					year_i = year_i + 1;
				}
			} else {
				date_i = date_i + daysNum;
				daysNum = 0;
			}
		}
		if (month_i < 10) {
			ms = "0" + month_i.toString();
		} else {
			ms = month_i.toString();
		}

		if (date_i < 10) {
			ds = "0" + date_i.toString();
		} else {
			ds = date_i.toString();
		}

		str = year_i.toString() + ms + ds;
		return str;
	}

	// 3. 获取某天之后若干天的YMD日期
	public static String getNextDaysYMD(String beginDate, int daysNums) {

		String str = null;
		int feb_days, month_days;
		int last_month_i, daysNum;

		String ys = beginDate.substring(0, 4);
		String ms = beginDate.substring(4, 6);
		String ds = beginDate.substring(6, 8);

		Integer year_i = Integer.parseInt(ys);
		Integer month_i = Integer.parseInt(ms);
		Integer date_i = Integer.parseInt(ds);

		daysNum = daysNums;

		while (daysNum > 0) {
			last_month_i = month_i - 1;
			if (last_month_i < 1)
				last_month_i = 12;

			if (((year_i % 400) == 0)
					|| ((year_i % 4) == 0 && (year_i % 100) != 0)) {
				feb_days = 29;
			} else {
				feb_days = 28;
			}

			switch (last_month_i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				month_days = 31;
				break;

			case 4:
			case 6:
			case 9:
			case 11:
				month_days = 30;
				break;

			case 2:
				month_days = feb_days;
				break;

			default:
				return (null);
			}

			if (daysNum >= date_i) {
				date_i = date_i + month_days;

				if (month_i == 1) {
					month_i = 12;
					year_i = year_i - 1;
				} else
					month_i = month_i - 1;
			} else {
				date_i = date_i - daysNum;
				daysNum = 0;
			}
		}

		if (month_i < 10) {
			ms = "0" + month_i.toString();
		} else {
			ms = month_i.toString();
		}

		if (date_i < 10) {
			ds = "0" + date_i.toString();
		} else {
			ds = date_i.toString();
		}

		str = year_i.toString() + ms + ds;
		return str;
	}

	// 获取某月之后若干月的YMD日期 mark =6 返回六位值 8返回8位值
	public static String getNextDaysYM(String ym, int count) {
		String str = ym.substring(0, 6);
		int mm = Integer.parseInt(str.substring(4, 6));

		int year = Integer.parseInt(str.substring(0, 4)) - count / 12;
		if (mm - count % 12 > 0) {
			mm = mm - count % 12;
		} else {
			year -= 1;
			mm = mm - count % 12 + 12;
		}
		String M = null;
		if (mm < 10) {
			M = "0" + String.valueOf(mm);
		} else {
			M = String.valueOf(mm);
		}
		return String.valueOf(year) + M;
	}

	// 1. 格式化日期 支持8位（fmtchar为3个长度字符串）年月日以及6位（fmtchar为2个长度字符串）的年月
	// 1. 格式化日期 支持8位（fmtchar为3个长度字符串）年月日以及6位（fmtchar为2个长度字符串）的年月
	public static String FormatsYMD(String ymd, String fmtchar) {

		StringBuilder builder = new StringBuilder();
		if (ymd.length() == 8) {
			if (fmtchar.length() == 2) {
				builder.append(ymd.substring(0, 4))
						.append(fmtchar.substring(0, 1))
						.append(ymd.substring(4, 6))
						.append(fmtchar.substring(1, 2))
						.append(ymd.substring(6));
			} else if (fmtchar.length() == 3) {
				builder.append(ymd.substring(0, 4))
						.append(fmtchar.substring(0, 1))
						.append(ymd.substring(4, 6))
						.append(fmtchar.substring(1, 2))
						.append(ymd.substring(6)).append(fmtchar.substring(2));
			}
		} else

		if (ymd.length() == 6) {

			if (fmtchar.length() == 1) {
				builder.append(ymd.substring(0, 4))
						.append(fmtchar.substring(0, 1))
						.append(ymd.substring(4));
			} else if (fmtchar.length() == 2) {
				builder.append(ymd.substring(0, 4))
						.append(fmtchar.substring(0, 1))
						.append(ymd.substring(4)).append(fmtchar.substring(1));
			}
		}
		return builder.toString();
	}

	/**
	 * byte[]转换为16进制的String 主要用以将生成的报文byte[]转化为String输出作为验证报文是否真确：
	 * 
	 * @param b1
	 * @return
	 */
	public static String byteToHexString(byte[] b1) {
		StringBuffer sb = new StringBuffer(b1.length * 2);
		String sTemp;
		for (int i = 0; i < b1.length; i++) {
			sTemp = Integer.toHexString(0xFF & b1[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 格式化时间xx:xx
	 * 
	 * @param time
	 * @return
	 */
	public static String formatTime(String time) {
		String strTime = time.substring(0, 2) + ":" + time.substring(2, 4);
		return strTime;
	}

	/**
	 * 格式化日期MM dd
	 * 
	 * @param ymd
	 * @return
	 */
	public static String YMD_return_MD(String ymd) {
		String reStr = ymd.substring(0, 4) + "年" + ymd.substring(4, 6) + "月"
				+ ymd.substring(6, 8) + "日";
		return reStr;
	}

	public static String getDurationString(int seconds) {

		int hours = seconds / 3600;
		int minutes = (seconds % 3600) / 60;
		seconds = seconds % 60;

		return twoDigitString(hours) + ":" + twoDigitString(minutes) + ":"
				+ twoDigitString(seconds);
	}

	public static String twoDigitString(int number) {

		if (number == 0) {
			return "00";
		}

		if (number / 10 == 0) {
			return "0" + number;
		}

		return String.valueOf(number);
	}

	/**
	 * 年月日星期
	 * @return
	 */
	public static String StringData() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期
		mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "日";
		} else if ("2".equals(mWay)) {
			mWay = "一";
		} else if ("3".equals(mWay)) {
			mWay = "二";
		} else if ("4".equals(mWay)) {
			mWay = "三";
		} else if ("5".equals(mWay)) {
			mWay = "四";
		} else if ("6".equals(mWay)) {
			mWay = "五";
		} else if ("7".equals(mWay)) {
			mWay = "六";
		}
		return mYear + "年" + mMonth + "月" + mDay + "日" + "    星期" + mWay;
	}

}
